#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <iostream>
#include "myOpenCVLIB\myheader.h"

using namespace std;  
using namespace cv; 


int main(){

	Mat img, padded,complex, mag;

	img = imread(LENA, CV_LOAD_IMAGE_GRAYSCALE);
	if(img.empty()) return -1;

	int m = getOptimalDFTSize(img.rows);
	int n = getOptimalDFTSize(img.cols);
	copyMakeBorder(img, padded, 0, m - img.rows, 0, n-img.cols,BORDER_CONSTANT,Scalar::all(0));
		namedWindow("copyMakeBorder");
		imshow("copyMakeBorder", img);
		waitKey();

	Mat planes [] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};
	
		namedWindow("planes");
		imshow("planes", planes[0]);
		waitKey();

	merge(planes,2,complex);
	
	
		imshow("planes", planes[0]);
		waitKey();


	dft(complex, complex);
	

	split(complex, planes);
		imshow("planes", planes[0]);
		waitKey();

	magnitude(planes[0], planes[1], planes[0]);
		imshow("planes", planes[0]);
		waitKey();
	mag = planes[0];
		namedWindow("mag");
		imshow("mag", mag);
		waitKey();

	mag += Scalar::all(1);
	log(mag,mag);

		imshow("mag", mag);
		waitKey();

	mag = mag(Rect(0,0,mag.cols & -2, mag.rows & -2));
		imshow("mag", mag);
		waitKey();
	int cx = mag.cols/2;
	int cy = mag.rows/2;


	Mat q0(mag,Rect(0,0,cx,cy));
	Mat q1(mag,Rect(cx,0,cx,cy));
	Mat q2(mag,Rect(0,cy,cx,cy));
	Mat q3(mag,Rect(cx,cy,cx,cy));

	Mat tmp;
	q0.copyTo(tmp);
	q3.copyTo(q0);
	tmp.copyTo(q3);

	q1.copyTo(tmp);
	q2.copyTo(q1);
	tmp.copyTo(q2);

	normalize(mag, mag, 0, 1, CV_MINMAX);


	namedWindow("image");
	namedWindow("asd");

	imshow("image", img);
	imshow("asd", mag);
	waitKey();

	return 0;
}
